﻿<div class="row">
    <div class="col-xs-2">
        <a href="#" id="btnAddDatabase">
            <img src="~/Images/database_add.png" title="Add Database" style="border: 0;" />
        </a>
        <a href="#" id="btnDeleteDatabase" style="display: none;">
            <img src="~/Images/database_delete.png" title="Delete Database" style="border: 0;" />
        </a>
    </div>
    <div class="col-xs-2">
        <a href="#" id="btnAddCollection" style="display: none;">
            <img src="~/Images/collection_add.png" title="Add Collection" style="border: 0;" />
        </a>
        <a href="#" id="btnDeleteCollection" style="display: none;">
            <img src="~/Images/collection_delete.png" title="Delete Collection" style="border: 0;" />
        </a>
    </div>
    <div class="col-xs-2">
        <a href="#" id="btnAddDocument" style="display: none;">
            <img src="~/Images/document_add.png" title="Add Document" style="border: 0;" />
        </a>
        <a href="#" id="btnAddDocumentFile" style="display: none;">
            <img src="~/Images/document_addfile.png" title="Add Document from file" style="border: 0;" />
        </a>
        <a href="#" id="btnDeleteDocument" style="display: none;">
            <img src="~/Images/document_delete.png" title="Delete Document" style="border: 0;" />
        </a>
    </div>
    <div class="col-xs-2 col-xs-push-4" style="text-align: right">
        <a href="#" id="btnExecuteQuery" class="btn btn-sm btn-primary" style="display: none;">
            <i class="glyphicon glyphicon-play" style="color: red"></i> Execute
        </a>
    </div>
</div>



<script>
    $("#btnAddDatabase").click(function () {
        $.ajax({
            type: "GET",            
            url: '@Url.Action("Create","Databases")',
            dataType: "html",
            success: function (result) {
                $("#pnlContent").html(result);
            },
            error: function (result) { showError('ERROR: ' + result.toString()); }
        });
    });

    $("#btnDeleteDatabase").click(function () {
        if (confirm("Are you sure you want to delete the Database '" + selectedDatabaseID + "'?\nThis action cannot be rolled back!"))
            deleteDatabase(selectedDatabaseLink);
    });

    $("#btnAddCollection").click(function () {
        $.ajax({
            type: "GET",
            data: { databaseLink: selectedDatabaseLink, databaseID: selectedDatabaseID },
            url: '@Url.Action("Create","Collections")',
            dataType: "html",
            success: function (result) {
                $("#pnlContent").html(result);
            },
            error: function (result) { showError('ERROR: ' + result.toString()); }
        });
    });

    $("#btnDeleteCollection").click(function () {
        if (confirm("Are you sure you want to delete the Collection '" + selectedCollectionID + "' ?")) {
            $.ajax({
                type: "POST",
                data: { databaseLink: selectedDatabaseLink, databaseID: selectedDatabaseID, collectionLink: selectedCollectionLink },
                url: '@Url.Action("Delete","Collections")',
                dataType: "Json",
                success: function (result) { CollectionDeleted(result); },
                error: function (result) { showError('ERROR: ' + result.toString()); }
            });
        }
    });

    function CollectionDeleted(result) {
        if (result.Success) {
            refreshCollections('@Url.Action("CollectionsList", "Collections")', result.Values.DatabaseLink, result.Values.DatabaseID);
        }
        else
            CollectionDeletionError(result.Message);
    }

    function CollectionDeletionError(data) {
        showError("Error!!!! " + data.toString());
    }

    $("#btnAddDocument").click(function () {
        $.ajax({
            type: "GET",
            data: { collectionLink: selectedCollectionLink, collectionID: selectedCollectionID },
            url: '@Url.Action("Create","Documents")',
            dataType: "html",
            success: function (result) {
                $("#pnlContent").html(result);
            },
            error: function (result) { showError('ERROR: ' + result.toString()); }
        });
    });

    $("#btnDeleteDocument").click(function () {
        if (confirm("Are you sure you want to delete the Document '" + selectedDocumentID + "'?\nThis action cannot be rolled back!"))
            deleteDocument(selectedDocumentLink);
    });

    $("#btnExecuteQuery").click(function () {
        ExecuteQuery($("#txtQueryText").val());
    });
</script>